const express = require("express");
const router = express.Router();
const {
  orderItemListModel,
  orderInfoListModel,
  orderRecordListModel,
  submitOrderParamsModel,
} = require("../model/order");
const { skuProductListModel } = require("../model/product");
const { leaderAddressModel } = require("../model/pickUpLocation");
const {
  cartInfoListModel,
  activityRuleModel,
  couponInfoListModel,
} = require("../model/cart");
router.get("/auth/orderInfo", async (req, res) => {
  const data = await orderRecordListModel.find();
  res.send({ data });
});
// 确认订单信息
router.get("/auth/confirmOrder", async (req, res) => {
  const data = await submitOrderParamsModel.find()
  console.log(data.length - 1);
  const datas = data[data.length - 1]
  // const data = await orderInfoListModel.findOne();
  const leaderAddressVo = await leaderAddressModel.find();
  const cartInfoList = await cartInfoListModel.find();
  const activityRule = await activityRuleModel.find();
  const couponInfoList = await couponInfoListModel.find();
  const carInfoVoList = [
    {
      activityRule,
      cartInfoList,
    },
  ];
  res.send({
    code: 200,
    msg: "获取成功",
    data: {
      activityReduceAmount: 2,
      couponReduceAmount: 3,
      originalTotalAmount: 120,
      totalAmount: 115,
      orderNo: 6.497177143428513e37,
      leaderAddressVo: leaderAddressVo[0],
      couponInfoList,
      carInfoVoList,
    },
    pop: data,
  });
});
router.get("/couponInfo/:_id/:couponStatus", async (req, res) => {
  const { _id, couponStatus } = req.params;
  if (couponStatus) {
    // await couponInfoListModel.updateMany({}, { couponStatus: 1 })
    await couponInfoListModel.updateMany({}, { isSelect: 1 });
    // await couponInfoListModel.updateOne({ _id }, { couponStatus: 0 })
    await couponInfoListModel.updateOne({ _id }, { isSelect: 0 });
    res.send({ code: 200, data: 200 });
  } else {
    // await couponInfoListModel.updateOne({ _id }, { couponStatus: 1 })
    res.send({ code: 200 });
  }
});
// 提交订单信息
router.post("/auth/submitOrder", async (req, res) => {
  console.log(req.body);
  await submitOrderParamsModel.create(req.body);
  const data = await submitOrderParamsModel.find();
  res.send({
    code: 200,
    msg: "提交成功",
    data,
  });
});

/*
 * 获取指定订单信息
 *  orderId -订单信息orderId
 * */
router.get("/auth/getOrderInfoById/:orderId", (req, res) => {
  res.send({
    code: 200,
    msg: "获取成功",
    data: [],
  });
});

/*
 *  查询用户订单列表
 *
 * */
router.get("/auth/findUserOrderPage/:page/:limit", async (req, res) => {
  const { limit, page } = req.query;
  let skip = (page - 1) * limit;
  console.log(req.query);
  const data = await orderRecordListModel.find().skip(skip).limit(limit);
  res.send({
    code: 200,
    msg: "获取成功",
    data: {
      total: 0,
      size: limit,
      current: page,
      pages: 1,
      records: data,
    },
  });
});

module.exports = router;
